package org.agile2soft.nexoma.tapestry.components;

import org.agile2soft.nexoma.core.controller.ServiceController;
import org.agile2soft.nexoma.core.controller.ServicesGroupController;
import org.agile2soft.nexoma.core.entity.Service;
import org.agile2soft.nexoma.core.entity.ServicesGroup;
import org.agile2soft.nexoma.tapestry.model.ApplicationContainer;
import org.agile2soft.nexoma.tapestry.model.SessionContainer;
import org.agile2soft.nexoma.tapestry.pages.Portal;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.IncludeJavaScriptLibrary;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.slf4j.Logger;

@IncludeJavaScriptLibrary({"context:resources/js/gwidgets_0.8.0/base.js",
                           "context:resources/js/gwidgets_0.8.0/gwidgets.js"
})
public class TabSetToolbar {

    @Inject
    private Logger logger;

    @Inject
    private ComponentResources resources;

    @Inject
    @Property
    private ApplicationContainer application;

    @Inject
    private ServicesGroupController serviceGroupController;

    @Inject
    private ServiceController serviceController;

    @SessionState
    @Property
    private SessionContainer session;

    @Property
    private ServicesGroup currentGroup;

    @Property
    private Service currentService;

    public String getCurrentClass() {

        ServicesGroup selectedGroup = session.getCurrentGroup();

        if ( null!=selectedGroup && selectedGroup.getId().equals(currentGroup.getId())) {
            return "tabset-option-selected";
        }
        else
        if ( currentGroup.getUrl().equalsIgnoreCase(resources.getPageName()) ) {
            return "tabset-option-selected";
        }
        return "tabset-option";
    }

    @OnEvent(component="selectGroup")
    Object doSelectGroup(Integer id) {
        session.setCurrentGroup(serviceGroupController.findById(id));
        logger.info("Group  "+session.getCurrentGroup().getName()+" selected");
        return session.getCurrentGroup().getUrl();
    }

    @OnEvent(component="init")
    Object doInit() {
        session.clearAll();
        return Portal.class;
    }
}
